package com.wangku.springmvc.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.wangku.springmvc.common.BaseController;
import com.wangku.springmvc.entity.Page;
import com.wangku.springmvc.entity.PageData;
import com.wangku.springmvc.service.BankService;
import com.wangku.springmvc.utils.AppResponse;
import com.wordnik.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 测试银行卡查询
 * @author: 李赛赛
 * @create: 2018-08-14 11:13
 **/
@Controller
@RequestMapping(value = "/SayController")
@Api(value = "SayController|一个用来测试swagger注解的控制器")
public class BankController extends BaseController {
    @Resource
    private BankService bankService;

    @ResponseBody
    @RequestMapping(value = "getUserById", method = RequestMethod.GET, produces = {"application/json; charset=utf-8", "application/xml"})
    @ApiOperation(value = "通过ID查询USER信息", httpMethod = "GET", notes = "暂无")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "deviceToken", value = "deviceToken", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "UserNumber", value = "用户编号", required = true, dataType = "Integer")
    })
    public AppResponse getUserById() {
        PageData user = this.getPageData();
        user.put("UserName", "测试人员");
        user.put("Age", 25);
        JSONObject object = new JSONObject();
        object.put("data", user);
        return AppResponse.success(object);
    }

    @ResponseBody
    @RequestMapping(value = "/all", method = RequestMethod.GET, produces = {"application/json; charset=utf-8", "application/xml"})
    @ApiOperation(value = "获取银行卡列表", httpMethod = "GET", notes = "仅1和2有正确返回")
    @ApiImplicitParam(paramType = "header", name = "deviceToken", value = "deviceToken", required = true, dataType = "String")
    public AppResponse findBank(Page page) throws Exception {
        JSONObject data = new JSONObject();
        List<PageData> fist = bankService.searchlistPage(page);
        if (fist.size() > 0) {
            data.put("fist", fist);
        }
        List<PageData> sceond = bankService.searchlist2Page(page);
        if (sceond.size() > 0) {
            data.put("sceond", sceond);
        }
        return AppResponse.success(data);
    }

    @ResponseBody
    @RequestMapping(value = "/updatePassword",method = RequestMethod.GET, produces = {"application/json; charset=utf-8", "application/xml"})
    @ApiOperation(value = "修改用户密码", notes = "根据用户id修改密码",httpMethod = "PUT")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "deviceToken", value = "deviceToken", required = true, dataType = "String"),
        @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID", required = true, dataType = "Integer"),
        @ApiImplicitParam(paramType = "query", name = "password", value = "旧密码", required = true, dataType = "String"),
        @ApiImplicitParam(paramType = "query", name = "newPassword", value = "新密码", required = true, dataType = "String")
    })
    public String updatePassword(@RequestParam(value = "userId") Integer userId, @RequestParam(value = "password") String password,
                                 @RequestParam(value = "newPassword") String newPassword) {
        if (userId <= 0 || userId > 2) {
            return "未知的用户";
        }
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(newPassword)) {
            return "密码不能为空";
        }
        if (password.equals(newPassword)) {
            return "新旧密码不能相同";
        }
        return "密码修改成功!";

    }
}